//
//  SQBaseViewController.m
//  pai
//
//  Created by pro on 14-10-29.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQBaseViewController.h"

@interface SQBaseViewController ()

@end

@implementation SQBaseViewController
@synthesize socketData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    asyncSocket = [[AsyncSocket alloc]initWithDelegate:self];
    [self.view setBackgroundColor:[UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1]];
}
-(void)showHUD
{
    self.HUD = nil;
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    self.HUD.margin = 10.f;
    self.HUD.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
    [self.HUD show:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)sendData:(NSData *)data tag:(NSInteger)tag withHUD:(BOOL)flag
{
    if (flag) {
        [self showHUD];
    }
    NSLog(@"sendData");
    self.socketData = [NSMutableData dataWithData:data];
    self.socketTag = tag;
    
    NSError* err = nil;
    if (asyncSocket.isConnected) {
        
        [asyncSocket writeData:self.socketData withTimeout:-1 tag:self.socketTag];
        [asyncSocket readDataWithTimeout:-1 tag:self.socketTag];
    }
    else{
        if(![asyncSocket connectToHost:SOCKETURL onPort:SOCKETPOINT error:&err])
        {
            NSLog(@"Error: %@", err);
        }
    }
}

#pragma AsyncSocket
//建立连接
-(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    NSLog(@"didConnectToHost");
    [asyncSocket writeData:self.socketData withTimeout:-1 tag:self.socketTag];
    [asyncSocket readDataWithTimeout:-1 tag:self.socketTag];
}
//发送数据
-(void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag
{
    NSLog(@"didWriteDataWithTag");
}
-(void)onSocket:(AsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag
{
    [asyncSocket readDataToLength:partialLength withTimeout:-1 tag:tag];
}
//////读取数据
//-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
//{
//    NSLog(@"didReadData");
//    [sock readDataWithTimeout:-1 tag:self.socketTag];
//}
//是否加密
-(void)onSocketDidSecure:(AsyncSocket *)sock
{
    NSLog(@"onSocketDidSecure");
}
//遇到错误时关闭连接
-(void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
    NSLog(@"willDisconnectWithError");
    if (self.HUD) {
        self.HUD.labelText = @"连接错误";
        [self.HUD showAnimated:YES whileExecutingBlock:^{
            sleep(3);
        } completionBlock:^{
            
        }];
    }
    //[[NSNotificationCenter  defaultCenter]postNotificationName:@"socketwillDisconnectWithError" object:nil];
}
//断开连接
-(void)onSocketDidDisconnect:(AsyncSocket *)sock
{
    NSLog(@"onSocketDidDisconnect");
}
@end
